"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/10 4:37 下午
@file: shaojie_controller
@Desc:
"""
from datetime import datetime, timedelta
from typing import List

from sqlalchemy import and_

from app.database import convert
from app.models.po.lugang.lugang_original_po import LuGangActivinessPredictPO

from flask import Blueprint, request, current_app
from app.controllers.response.response import SuccessResult
from app.exts import ironman_redis as redis
import logging

bp = Blueprint('lugang_short_activiness', __name__, url_prefix="/api/v1/lugang/act")


@bp.route('/data2', methods=['GET'])
def mock_lugang_activiness_data():
    import random

    for key in ['CG_LT_GL_GL04_FL', 'CG_LT_GL_GL04_RLB', 'CG_LT_GL_GL04_LDWD01', 'CG_LT_GL_GL04_LDWD02',
                'CG_LT_GL_GL04_LDWD03', 'CG_LT_GL_GL04_LDWD04', 'CG_LT_GL_GL04_DWJZ', 'CG_LT_GL_GL04_TSWD',
                'CG_LT_GL_GL04_LFMQL', 'CG_LT_GL_GL04_FYL', 'CG_LT_GL_GL04_FYFZ', 'CG_LT_GL_GL04_m40',
                'CG_LT_GL_GL04_CSR', 'CG_LT_GL_GL04_25', 'CG_LT_GL_GL04_40', 'CG_LT_GL_GL04_60', 'CG_LT_GL_GL04_80',
                'CG_LT_GL_GL04_JTJZ']:
        redis.hset("lugang_activiness_short", key, random.randint(100, 200))
        redis.hset("lugang_activiness_short_update", key, datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S"))
        logging.info(key)
    return SuccessResult(detail="ok")


@bp.route('/short', methods=['GET'])
def lugang_activiness_short():
    start = request.args.get("start")
    end = request.args.get("end")

    start = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
    end = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")

    records: List[LuGangActivinessPredictPO] = convert.query(LuGangActivinessPredictPO,
                                                             LuGangActivinessPredictPO.date_time >= start,
                                                             LuGangActivinessPredictPO.date_time <= end,
                                                             LuGangActivinessPredictPO.level == 'hour',
                                                             LuGangActivinessPredictPO.type == 'short').all()
    if len(records) > 0:
        current = [record.current for record in records]
        current.append(None)
        predict = [record.predict for record in records]

        perv_id = records[0].id
        prev = convert.query(LuGangActivinessPredictPO,
                             and_(LuGangActivinessPredictPO.id < perv_id,
                                  LuGangActivinessPredictPO.level == 'hour',
                                  LuGangActivinessPredictPO.type == 'short'),
                             order_by=LuGangActivinessPredictPO.id.desc()).first()

        if prev:
            predict.insert(0, prev.predict)
        else:
            predict.insert(0, None)

        next_hour = records[len(records) - 1].date_time + timedelta(hours=1)
        date = [record.date_time for record in records]
        date.append(next_hour)
        date = [d.strftime("%Y-%m-%d %H") for d in date]

        return SuccessResult(detail=dict(current=current, predict=predict, date=date))
    return SuccessResult(detail=[])
